package com.hzlj.position.job.controller;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.AddCommonDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.exception.PubException;
import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.common.message.common.util.MessageRestUtil;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindQueryResultDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindSimpleQueryParamDTO;
import com.hzlj.position.config.common.enums.ChannelType;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import com.hzlj.position.job.job.OperatorTimerTaskJob;
import com.hzlj.position.job.job.WdTimerTaskJob;
import com.hzlj.position.locate.common.dto.position.PositionAddResultDTO;
import com.hzlj.position.locate.common.dto.position.PositionQueryResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 【定位记录-分表】接口
 *
 * @author lifh
 * @date 2023-03-28 21:11:50
 */
@Slf4j
@RestController
@RequestMapping("/position/locate")
public class PositionFetchController {
    @Resource
    private OperatorTimerTaskJob operatorTimerTaskJob;
    @Resource
    private WdTimerTaskJob wdTimerTaskJob;
    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;

    /**
     * 运营商-定位
     */
    @GetMapping("/operator/locate")
    public ResultDTO<PositionQueryResultDTO> operatorLocate(String deviceCode, String channel) {
        log.info("手工发起运营商定位：deviceCode={},channel={}", deviceCode, channel);
        List<JzJbxxDeviceBindQueryResultDTO> exists = jzJbxxDeviceBindService.listJzJbxxDeviceBind(
                new JzJbxxDeviceBindSimpleQueryParamDTO().setDeviceCode(deviceCode).setChannel(channel)
        );
        if (ObjectUtil.isEmpty(exists)) {
            throw new PubException("没有绑定记录，请确认是否绑定该设备");
        }

        JzJbxxDeviceBindQueryResultDTO bind = exists.get(0);
        PositionAddResultDTO result = operatorTimerTaskJob.fetchPosition(bind.getJgCode(), bind.getJzId(), deviceCode, channel);
        if (result.isSuccess()) {
            return ResultDTO.ok(result.getPosition());
        }
        return ResultDTO.failed(result.getCode(), result.getMsg());
    }


    /**
     * 运营商-定位(根据单位批量)
     * 会修改定位剩余间隔
     */
    @GetMapping("/operator/locate/byJgCode")
    public ResultDTO<Boolean> operatorLocate(String jgCode,
                                             AddCommonDTO<String> operator) {
        JzJbxxDeviceBindSimpleQueryParamDTO dto = new JzJbxxDeviceBindSimpleQueryParamDTO()
                .setChannelType(ChannelType.MOBILE.getCode());
        dto.setJgCode(jgCode);
        log.info("手工发起运营商定位：jgCode={},operator={}", jgCode, operator);
        List<JzJbxxDeviceBindQueryResultDTO> exists = jzJbxxDeviceBindService.listJzJbxxDeviceBind(dto);
        if (ObjectUtil.isEmpty(exists)) {
            throw new PubException("该单位没有需要定位的记录");
        }
        for (JzJbxxDeviceBindQueryResultDTO bind : exists) {
            operatorTimerTaskJob.addTask(
                    bind.getJgCode(),
                    bind.getJzId(),
                    bind.getDeviceCode(),
                    bind.getChannel(),
                    bind.getLocateInterval());

        }
        return ResultDTO.ok();
    }


    /**
     * APP-定位
     */
    @GetMapping("/app/locate")
    public ResultDTO<MessageNoticeResultDTO> appLocate(String jzId) {
        log.info("手工发起App定位：)={}", jzId);
        //检查设备绑定信息
        jzJbxxDeviceBindService.getJzJbxxDeviceBindByJzId(jzId, ChannelType.APP.getCode());
        MessageNoticeResultDTO noticeResult = MessageRestUtil.noticeQueryUsers(
                "free-fill-websocket-jzry",
                null,
                MapUtil.<String, Object>builder("content", "{\"cmd\":\"locate\"}").build(),
                false,
                jzId
        );
        if (noticeResult.isFail()) {
            log.warn("获取实时定位失败(app):jzId={},result={}", jzId, JsonUtils.obj2json(noticeResult));
        }
        return new ResultDTO<>(noticeResult);
    }

    /**
     * WD-定位
     */
    @GetMapping("/wd/locate")
    public ResultDTO<PositionQueryResultDTO> wdLocate(String deviceCode, String channel) {
        log.info("手工发起腕带定位：deviceCode={},channel={}", deviceCode, channel);
        List<JzJbxxDeviceBindQueryResultDTO> exists = jzJbxxDeviceBindService.listJzJbxxDeviceBind(
                new JzJbxxDeviceBindSimpleQueryParamDTO().setDeviceCode(deviceCode).setChannel(channel)
        );
        if (ObjectUtil.isEmpty(exists)) {
            throw new PubException("没有绑定记录，请确认是否绑定该设备");
        }

        JzJbxxDeviceBindQueryResultDTO bind = exists.get(0);
        PositionAddResultDTO result = wdTimerTaskJob.fetchPositionNewest(bind.getJgCode(), bind.getJzId(), deviceCode, channel);
        if (result.isSuccess()) {
            return ResultDTO.ok(result.getPosition());
        }
        return ResultDTO.failed(result.getCode(), result.getMsg());
    }
}

